var results_array = [];
var url = '/api/ajax_delete_resume.cfm';
var progress_bar = function(wd,ht,txt,cl) {
	var throwaway = '<img src="/images/progress.gif" id="progress_bar" width="'+wd+'" height="'+ht+'" alt="'+txt+'" title="'+txt+'" align="absmiddle" style="padding:3px;"/>';	
	return throwaway;
};
var loading_bar = function(x,ele) {
	(x) ? $("#"+ele).html(progress_bar(16,16,'','blue')) : $("#"+ele).html('');
};
var loading = function() {return '<span style="vertical-align:middle;"> '+progress_bar(16,16,'','')+'</span>';
};

var get_areas = function() {
  
  $.ajax({
    type: 'GET',
    url: '/api/ajax_get_areas.cfm',
    dataType: "json",
    success: function(json) {
      results_array = json.area;
    },
    complete: function(json) {

    },
    error: function(json) {                
    }                
  });
}

var five_star = function() {
	$('#loading').html(''+loading() );
  
  $.ajax({
    type: 'GET',
    url: '/api/ajax_get_five_star.cfm',
    dataType: "json",
    success: function(json) {
      results_array = json.area;
    },
    complete: function(json) {
        var results_text = '';
        results_text += '<select id="five_star_area" class="search_select"><option value="0" selected> </option>';
        $.map(results_array,function(n) { 
            results_text += '<option value="'+n.id+'">'+n.area+'</option>';        
        });
        results_text += '</select >';
        $('#fsa').html(results_text);
                
        $("#five_star_area").bind("change", function(e){
            get_sub($('#five_star_area').val());
        });
        
        connectFunc();
        $('#loading span').remove();
    },
    error: function(json) {                
    }                
  });
}

var get_sub = function(id) {
    var results_text = '';	
    var local_array = [];	
	$('#loading').html(''+loading() );
	$.ajax({
    type: 'GET',
    url: '/api/ajax_delete_resume.cfm?action=get_sub&id='+id,
    dataType: "json",
    success: function(json) {
        local_array = json.area;
    },
    complete: function(json) {
        $('#fsa').html('<select id="five_star_area" class="search_select"></select >')
        
        $.map(results_array,function(n) {
           $("#five_star_area").append('<option value="'+n.id+'">'+n.area+'</option>');
        });        
        $("#five_star_area option[value='"+id+"']").attr("selected","selected");
        
        results_text += '&nbsp;';
        results_text += '<select id="five_star_area_sub" class="search_select">';        
        $.map(local_array,function(n) {
            results_text += '<option value="'+n.sub_id+'">'+n.sub_area+'</option>';
        });		

        results_text += '</select >';
        $('#fsa').append(results_text) ;
        $("#five_star_area").bind("change", function(e){
            get_sub($('#five_star_area').val());
        });    
        $("#five_star_area_sub").bind("change", function(e){
            add_area($('#five_star_area_sub').val());
        });
        $('#loading span').remove();
    },
    error: function(json) {                
    }                
  });
}

var add_area = function(id) {
    var local_array = [];
	$('#loading').html(''+loading() );
	
	$.ajax({
        type: 'GET',
        url: '/api/ajax_delete_resume.cfm?action=add_sub&id='+id,
        dataType: "json",
        success: function(json) {
            local_array = json.area;
            
        },
        complete: function(json) {
            var x = '';
            x += local_array[0].sub_area + ',';
            var y = '';
            y += local_array[0].sub_id + ',';
            $("#sub_area_list").val($("#sub_area_list").val() + local_array[0].sub_area + ',') ;
            $("#sid_list").val( $("#sid_list").val()  + local_array[0].sub_id + ',' )  ;	
	
            connectFunc();
            $('#loading span').remove();
        },
        error: function(json) {
        
        }
     });
}

var connectFunc = function() {	

	var local_array = [];
	$.ajax({
        type: 'GET',
        url: '/api/ajax_delete_resume.cfm?action=make_list&sid_list='+$("#sid_list").val()+'&sub_area_list='+$("#sub_area_list").val(),
        dataType: "json",
        success: function(json) {
          local_array = json.area;          
        },
        complete: function(json) {
            
            $.map(results_array,function(n) {
                $("#parent_area_"+n.id).html('');
            });
            $.map(local_array,function(n) {
                $("#parent_area_"+n.parent_id).html(n.parent_id_list);
            });
            $.map(local_array,function(n) {
                $("#sub_"+n.sub_id).bind('click', function(e) {
                     delete_list(n.sub_id,n.sub_area,n.parent_id);
                });
            });
        },
        error: function(json) {                
        }                
   });
	
}

var delete_list = function(id,area,parent_id) {
	$('#loading').html(''+loading() );
	var local_array = [];
	var results_text = '';
	$.ajax({
    type: 'GET',
    url: '/api/ajax_delete_resume.cfm?action=delete_list&id='+id+'&area='+area+'&sub_list='+$('#sid_list').val()+'&parent_id='+parent_id+'&sub_area_list='+$('#sub_area_list').val(),
    dataType: "json",
    success: function(json) {
      local_array = json.area;
    },
    complete: function(json) {
        results_text = '';
	
        if(local_array[0].sub_area_list == ''){
        $("#sub_area_list").val('');	
        }else{
        $("#sub_area_list").val(local_array[0].sub_area_list + ',');	
        }
        if(local_array[0].sub_list == ''){
        $("#sid_list").val('');	
        }else{
        $("#sid_list").val(local_array[0].sub_list + ',');	
        }
        if(local_array[0].sub_list == ''){
        $('#parent_area_' + local_array[0].deleted_pid).html('') ;	
        }
        
        $('#loading span').remove();
        connectFunc();
    },
    error: function(json) {                
    }                
  });
}

var start_search = function() {
	var local_array = [];
	$('#ajax_poc_id').val($('#poc_id').val());
	$.ajax({
    type: 'GET',
    url: '/api/ajax_delete_resume.cfm?action=get_resumes&id='+$('#ajax_poc_id').val(),
    dataType: "json",
    success: function(json) {
	  $('#search_results_area').html(loading());
      local_array = json.files;
    },
    complete: function(json) {
		$('#search_results_area').html('');
	    $.map(local_array, function(n) {
			$("#search_results_area").append('<a href="'+n.resume_location+'" id="" class="" >'+n.resume_title+'</a>&nbsp;<img src="/images/image_delete.gif" id="resume_'+n.resume_id+'" align="absmiddle" style=" cursor:pointer;" /><br>');
			$("#resume_"+n.resume_id).bind("click", function() {
				deleteResume(n.resume_id);										  
			});
		});	          
    },
    error: function(json) {                
    }                
  });
}

var deleteResume = function(id) {
	$.ajax({
    type: 'GET',
    url: '/api/ajax_delete_resume.cfm?action=delete_resumes&id='+id,
    dataType: "json",
    success: function(json) {

    },
    complete: function(json) {
	   start_search();       
    },
    error: function(json) {                
    }                
  });
}

function validate_form() {
    $("#error_save").html();
	var allFieldList = ['phone','alt_phone','yrs_five_star_area_experience','yrs_category_experience','email','zip'];
	$.map(allFieldList,function(n) {
        $("#error_"+n).html();
	});
    var error_count = 0;
    var error = ''; 
    var phoneFieldList = ['phone','alt_phone'];
	var zipFieldList = ['zip'];
	var emailFieldList = ['email'];
	var numberFieldList = ['yrs_five_star_area_experience','yrs_category_experience'];
	$.map(phoneFieldList,function(n) {
        if( !validPhone($("#"+n).val() ) && $("#"+n).val() != "" ){
        $("#error_"+n).html('Invalid Phone number format. (i.e. xxx-xxx-xxxx)');
        error_count++;
        }
	});
	$.map(zipFieldList,function(n) {
        if( !validZip($("#"+n).val() ) && $("#"+n).val() != ""  ){
        $("#error_"+n).html('Invalid Zipcode format. (i.e. xxxxx)');
        error_count++;
        }
	});	
	$.map(emailFieldList,function(n) {
        if( !verifyEmail($("#"+n).val() ) && $("#"+n).val() != ""  ){
        $("#error_"+n).html('Invalid Email format. (i.e. xxx@xx.xx)');
        error_count++;
        }
	});	
    if(error_count){
    
    $("#error_save").html('<img src="/images/emblem-important.png" width="22" height="22" align="absmiddle" title="Warning"alt="Warning"> Can\'t save!  Please review errors!');
    }else {
        return true;
    }
	loading_bar(false,'edit_loading');
}
function validPhone(phoneString) {
    var re = new RegExp(/^\d\d\d\-\d\d\d\-\d\d\d\d/g);
    var check = re.exec(phoneString);
    if (check == null) {
        return false;
    }
    return phoneString;
}
function validZip(phoneString) {
    var re = new RegExp(/^\d\d\d\d\d/g);
    var check = re.exec(phoneString);
    if (check == null) {
        return false;
    }
    return phoneString;
}
function verifyEmail(email) {
	var rx = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?";
  if( email.match(rx) ){
      return email;
  } 
  return false;
}
$(function() {
	Functional.install();
	
	get_areas();
	five_star();
	start_search();
	$('#tf_start').datepicker({ dateFormat: 'mm/dd/yy' });
	$('#tf_end').datepicker({ dateFormat: 'mm/dd/yy' });
	
	$("#myform").submit(function() {
      if (validate_form()) {
       
        loading_bar(1,'edit_loading');
        
        return true;
      }
     return false;
    });
	
});